home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / amiga / plotting / gnuplot3.lzh / gnuplot / plot.c < prev    next >
C/C++ Source or Header  |  1991-09-05  |  9KB  |  342 lines

  1. /* GNUPLOT - plot.c */
  2. /*
  3.  * Copyright (C) 1986, 1987, 1990, 1991   Thomas Williams, Colin Kelley
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software is provided "as is" without express or implied warranty.
  16.  * 
  17.  *
  18.  * AUTHORS
  19.  * 
  20.  *   Original Software:
  21.  *     Thomas Williams,  Colin Kelley.
  22.  * 
  23.  *   Gnuplot 2.0 additions:
  24.  *       Russell Lang, Dave Kotz, John Campbell.
  25.  *
  26.  *   Gnuplot 3.0 additions:
  27.  *       Gershon Elber and many others.
  28.  * 
  29.  * Send your comments or suggestions to 
  30.  *  pixar!info-gnuplot@sun.com.
  31.  * This is a mailing list; to join it send a note to 
  32.  *  pixar!info-gnuplot-request@sun.com.  
  33.  * Send bug reports to
  34.  *  pixar!bug-gnuplot@sun.com.
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <setjmp.h>
  39. #include <signal.h>
  40. #include "plot.h"
  41. #include "setshow.h"
  42. #ifdef MSDOS
  43. #include <io.h>
  44. #endif
  45. #ifdef vms
  46. #include <unixio.h>
  47. #include <smgdef.h>
  48. extern int vms_vkid;
  49. extern smg$create_virtual_keyboard();
  50. unsigned int status[2] = {1, 0};
  51. #endif
  52. #ifdef AMIGA_LC_5_1
  53. #include <proto/dos.h>
  54. #endif
  55.  
  56. #ifdef __TURBOC__
  57. #include <graphics.h>
  58. #endif
  59.  
  60. extern char *getenv(),*strcat(),*strcpy(),*strncpy();
  61.  
  62. extern char input_line[];
  63. extern int c_token;
  64. extern FILE *outfile;
  65. extern int term;
  66.  
  67. BOOLEAN interactive = TRUE;    /* FALSE if stdin not a terminal */
  68. char *infile_name = NULL;    /* name of command file; NULL if terminal */
  69.  
  70. #ifndef STDOUT
  71. #define STDOUT 1
  72. #endif
  73.  
  74. jmp_buf env;
  75.  
  76. struct value *integer(),*complex();
  77.  
  78.  
  79. extern f_push(),f_pushc(),f_pushd1(),f_pushd2(),f_call(),f_call2(),f_lnot(),f_bnot(),f_uminus()
  80.     ,f_lor(),f_land(),f_bor(),f_xor(),f_band(),f_eq(),f_ne(),f_gt(),f_lt(),
  81.     f_ge(),f_le(),f_plus(),f_minus(),f_mult(),f_div(),f_mod(),f_power(),
  82.     f_factorial(),f_bool(),f_jump(),f_jumpz(),f_jumpnz(),f_jtern();
  83.  
  84. extern f_real(),f_imag(),f_arg(),f_conjg(),f_sin(),f_cos(),f_tan(),f_asin(),
  85.     f_acos(),f_atan(),f_sinh(),f_cosh(),f_tanh(),f_int(),f_abs(),f_sgn(),
  86.     f_sqrt(),f_exp(),f_log10(),f_log(),f_besj0(),f_besj1(),f_besy0(),f_besy1(),
  87. #ifdef GAMMA
  88.     f_gamma(),
  89. #endif
  90.     f_floor(),f_ceil();
  91.  
  92.  
  93. struct ft_entry ft[] = {    /* built-in function table */
  94.  
  95. /* internal functions: */
  96.     {"push", f_push},    {"pushc", f_pushc},
  97.     {"pushd1", f_pushd1},    {"pushd2", f_pushd2},
  98.     {"call", f_call},    {"call2", f_call2},    {"lnot", f_lnot},
  99.     {"bnot", f_bnot},    {"uminus", f_uminus},    {"lor", f_lor},
  100.     {"land", f_land},    {"bor", f_bor},        {"xor", f_xor},
  101.     {"band", f_band},    {"eq", f_eq},        {"ne", f_ne},
  102.     {"gt", f_gt},        {"lt", f_lt},        {"ge", f_ge},
  103.     {"le", f_le},        {"plus", f_plus},    {"minus", f_minus},
  104.     {"mult", f_mult},    {"div", f_div},        {"mod", f_mod},
  105.     {"power", f_power}, {"factorial", f_factorial},
  106.     {"bool", f_bool},    {"jump", f_jump},    {"jumpz", f_jumpz},
  107.     {"jumpnz",f_jumpnz},{"jtern", f_jtern},
  108.  
  109. /* standard functions: */
  110.     {"real", f_real},    {"imag", f_imag},    {"arg", f_arg},
  111.     {"conjg", f_conjg}, {"sin", f_sin},        {"cos", f_cos},
  112.     {"tan", f_tan},        {"asin", f_asin},    {"acos", f_acos},
  113.     {"atan", f_atan},    {"sinh", f_sinh},    {"cosh", f_cosh},
  114.     {"tanh", f_tanh},    {"int", f_int},        {"abs", f_abs},
  115.     {"sgn", f_sgn},        {"sqrt", f_sqrt},    {"exp", f_exp},
  116.     {"log10", f_log10},    {"log", f_log},        {"besj0", f_besj0},
  117.     {"besj1", f_besj1},    {"besy0", f_besy0},    {"besy1", f_besy1},
  118. #ifdef GAMMA
  119.      {"gamma", f_gamma},
  120. #endif
  121.     {"floor", f_floor},    {"ceil", f_ceil},
  122.     {NULL, NULL}
  123. };
  124.  
  125. static struct udvt_entry udv_pi = {NULL, "pi",FALSE};
  126.                                     /* first in linked list */
  127. struct udvt_entry *first_udv = &udv_pi;
  128. struct udft_entry *first_udf = NULL;
  129.  
  130.  
  131.  
  132. #ifdef vms
  133.  
  134. #define HOME "sys$login:"
  135.  
  136. #else /* vms */
  137. #ifdef MSDOS
  138.  
  139. #define HOME "GNUPLOT"
  140.  
  141. #else /* MSDOS */
  142.  
  143. #if defined(AMIGA_AC_5) || defined(AMIGA_LC_5_1)
  144.  
  145. #define HOME "GNUPLOT"
  146. #else /* AMIGA */
  147.  
  148. #define HOME "HOME"
  149.  
  150. #endif /* AMIGA */
  151. #endif /* MSDOS */
  152. #endif /* vms */
  153.  
  154. #ifdef unix
  155. #define PLOTRC ".gnuplot"
  156. #else /* unix */
  157. #if defined(AMIGA_AC_5) || defined(AMIGA_LC_5_1)
  158. #define PLOTRC ".gnuplot"
  159. #else /* AMIGA */
  160. #define PLOTRC "gnuplot.ini"
  161. #endif /* AMIGA */
  162. #endif /* unix */
  163.  
  164. #ifdef __TURBOC__
  165. void tc_interrupt()
  166. #else
  167. #ifdef _CRAY
  168. void inter(an_int)
  169. int an_int;
  170. #else
  171. inter()
  172. #endif
  173. #endif
  174. {
  175. #ifdef MSDOS
  176. #ifdef __TURBOC__
  177.     (void) signal(SIGINT, tc_interrupt);
  178. #else
  179.     void ss_interrupt();
  180.     (void) signal(SIGINT, ss_interrupt);
  181. #endif
  182. #else  /* MSDOS */
  183.     (void) signal(SIGINT, inter);
  184. #endif  /* MSDOS */
  185.     (void) signal(SIGFPE, SIG_DFL);    /* turn off FPE trapping */
  186.     if (term && term_init)
  187.         (*term_tbl[term].text)();    /* hopefully reset text mode */
  188.     (void) fflush(outfile);
  189.     (void) putc('\n',stderr);
  190.     longjmp(env, TRUE);        /* return to prompt */
  191. }
  192.  
  193.  
  194. main(argc, argv)
  195.     int argc;
  196.     char **argv;
  197. {
  198. /* Register the Borland Graphics Interface drivers. If they have been */
  199. /* included by the linker.                                            */
  200.  
  201. #ifdef __TURBOC__
  202. registerfarbgidriver(EGAVGA_driver_far);
  203. registerfarbgidriver(CGA_driver_far);
  204. registerfarbgidriver(Herc_driver_far);
  205. registerfarbgidriver(ATT_driver_far);
  206. #endif
  207. #ifdef X11
  208.      { int n = X11_args(argc, argv); argv += n; argc -= n; }
  209. #endif 
  210.  
  211. #ifdef apollo
  212.     apollo_pfm_catch();
  213. #endif
  214.  
  215.     setbuf(stderr,(char *)NULL);
  216.     outfile = stdout;
  217.     (void) complex(&udv_pi.udv_value, Pi, 0.0);
  218.  
  219.      interactive = FALSE;
  220.      init_terminal();        /* can set term type if it likes */
  221.  
  222. #ifdef AMIGA_LC_5_1
  223.      if (IsInteractive(Input()) == DOSTRUE) interactive = TRUE;
  224.      else interactive = FALSE;
  225. #else
  226.      interactive = isatty(fileno(stdin));
  227. #endif
  228.      if (argc > 1)
  229.       interactive = FALSE;
  230.  
  231.      if (interactive)
  232.       show_version();
  233. #ifdef vms   /* initialise screen management routines for command recall */
  234.           if (status[1] = smg$create_virtual_keyboard(&vms_vkid) != SS$_NORMAL)
  235.                done(status[1]);
  236. #endif
  237.  
  238.     if (!setjmp(env)) {
  239.         /* first time */
  240.         interrupt_setup();
  241.         load_rcfile();
  242.  
  243.         if (interactive && term != 0)    /* not unknown */
  244.          fprintf(stderr, "\nTerminal type set to '%s'\n", 
  245.                 term_tbl[term].name);
  246.     } else {    
  247.         /* come back here from int_error() */
  248.         load_file_error();    /* if we were in load_file(), cleanup */
  249. #ifdef vms
  250.         /* after catching interrupt */
  251.         /* VAX stuffs up stdout on SIGINT while writing to stdout,
  252.           so reopen stdout. */
  253.         if (outfile == stdout) {
  254.            if ( (stdout = freopen("SYS$OUTPUT","w",stdout))  == NULL) {
  255.               /* couldn't reopen it so try opening it instead */
  256.               if ( (stdout = fopen("SYS$OUTPUT","w"))  == NULL) {
  257.                  /* don't use int_error here - causes infinite loop! */
  258.                  fprintf(stderr,"Error opening SYS$OUTPUT as stdout\n");
  259.               }
  260.            }
  261.            outfile = stdout;
  262.         }
  263. #endif                    /* VMS */
  264.         if (!interactive)
  265.          done(IO_ERROR);            /* exit on non-interactive error */
  266.     }
  267.  
  268.      if (argc > 1) {
  269.         /* load filenames given as arguments */
  270.         while (--argc > 0) {
  271.            ++argv;
  272.            c_token = NO_CARET; /* in case of file not found */
  273.            load_file(fopen(*argv,"r"), *argv);    
  274.         }
  275.     } else {
  276.         /* take commands from stdin */
  277.         while(TRUE)
  278.          com_line();
  279.     }
  280.  
  281.      done(IO_SUCCESS);
  282. }
  283.  
  284. /* Set up to catch interrupts */
  285. interrupt_setup()
  286. {
  287. #ifdef MSDOS
  288. #ifdef __TURBOC__
  289.         (void) signal(SIGINT, tc_interrupt);    /* go there on interrupt char */
  290. #else
  291.         void ss_interrupt();
  292.         save_stack();                /* work-around for MSC 4.0/MSDOS 3.x bug */
  293.         (void) signal(SIGINT, ss_interrupt);
  294. #endif
  295. #else /* MSDOS */
  296.         (void) signal(SIGINT, inter);    /* go there on interrupt char */
  297. #endif /* MSDOS */
  298. }
  299.  
  300.  
  301. /* Look for a gnuplot start-up file */
  302. load_rcfile()
  303. {
  304.     register FILE *plotrc;
  305.     static char home[80];
  306.     static char rcfile[sizeof(PLOTRC)+80];
  307.  
  308.     /* Look for a gnuplot init file in . or home directory */
  309. #ifdef vms
  310.     (void) strcpy(home,HOME);
  311. #else /* vms */
  312. #if defined(AMIGA_AC_5) || defined(AMIGA_LC_5_1)
  313.     strcpy(home,getenv(HOME));
  314.     {
  315.         int h;
  316.         h = strlen(home) - 1;
  317.         if (h >= 0) {
  318.             if ((home[h] != ':') && (home[h] != '/')) {
  319.                 home[h] = '/';
  320.                 home[h+1] = '\0';
  321.             }
  322.            }
  323.     }
  324. #else /* AMIGA */
  325.     (void) strcat(strcpy(home,getenv(HOME)),"/");
  326. #endif /* AMIGA */
  327. #endif /* vms */
  328. #ifdef NOCWDRC
  329.     /* inhibit check of init file in current directory for security reasons */
  330.     {
  331. #else
  332.     (void) strcpy(rcfile, PLOTRC);
  333.     plotrc = fopen(rcfile,"r");
  334.     if (plotrc == (FILE *)NULL) {
  335. #endif
  336.        (void) sprintf(rcfile, "%s%s", home, PLOTRC);
  337.        plotrc = fopen(rcfile,"r");
  338.     }
  339.     if (plotrc)
  340.      load_file(plotrc, rcfile);
  341. }
  342.